package com.threegroup.android3grouppersonality.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.threegroup.android3grouppersonality.R;
import com.threegroup.android3grouppersonality.pojo.User;
import com.threegroup.android3grouppersonality.utils.HttpClientUtils;
import com.threegroup.android3grouppersonality.utils.Tool;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_loginOK;//登录
    private Button btn_loginCancel;//取消输入内容
    private EditText et_loginUserPhone;//登录的手机号
    private EditText et_loginUserPwd;//登录的密码
    private TextView tv_loginForget;//忘记密码
    private SharedPreferences sp;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        btn_loginOK=findViewById(R.id.btn_loginOK);
        btn_loginCancel=findViewById(R.id.btn_loginCancel);
        et_loginUserPhone=findViewById(R.id.et_loginUserPhone);
        et_loginUserPwd=findViewById(R.id.et_loginUserPwd);
        tv_loginForget=findViewById(R.id.tv_loginForget);

        btn_loginOK.setOnClickListener(this);
        btn_loginCancel.setOnClickListener(this);
        tv_loginForget.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_loginOK://成功→首页
                login();

                break;
            case R.id.btn_loginCancel:
                setNullText();
                break;
        }
    }

    private void login() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, Object> txtmap = new HashMap<>();
                    txtmap.put("phone", et_loginUserPhone.getText().toString());
                    txtmap.put("password", et_loginUserPwd.getText().toString());
                    String login = HttpClientUtils.HttpClientPost(Tool.url + "user/login", txtmap);
                    Message message = new Message();
                    message.obj = login;
                    handler.sendMessage(message);
                    message.what = Tool.LOGIN_SUCCESS;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //接收服务器传回的值，判断账号密码是否正确
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Tool.LOGIN_SUCCESS) {
                String str = msg.obj.toString();
                if (str.equals("error")) {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    LoginActivity.this.startActivity(intent);
                    //将用户信息传入本地
                    Gson gson = new Gson();
                    User user= gson.fromJson(str, User.class);
                    String name=user.getName();
                    String introduction=user.getIntroduction();
                    String sex=user.getSex();
                    String photo=user.getPhoto();
                    sp = getSharedPreferences("share", MODE_PRIVATE);
                    SharedPreferences.Editor ed = sp.edit();
                    ed.putString("phone", et_loginUserPhone.getText().toString());
                    ed.putString("password",et_loginUserPwd.getText().toString());
                    ed.putString("name",name);
                    ed.putString("introduction",introduction);
                    ed.putString("sex",sex);
                    ed.putString("photo",photo);
                    ed.commit();
                }
            }
        }
    };

    private void setNullText() {//将输入的内容设置为""
        et_loginUserPhone.setText("");
        et_loginUserPwd.setText("");
    }
}
